package com.mall.controller;

import com.mall.bean.SystemPermission;
import com.mall.service.SystemPermissionService;
import com.mall.model.ResponseBean;
import com.mall.service.SystemRolePermissionService;
import com.mall.vo.PermissionVo;
import com.mall.vo.Response;
import com.sun.deploy.net.HttpRequest;
import com.sun.deploy.net.HttpResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PageName com.mall.controller
 * @Description 权限控制层
 * @Author 何全裕
 * @Create 2017-09-03 22:52
 * @Version 1.0
 **/
@Controller
@RequestMapping("/permission")
public class SystemPermissionController {

    @Autowired
    private SystemPermissionService systemPermissionService;

    @Autowired
    private SystemRolePermissionService systemRolePermissionServiceImpl;

    @RequestMapping("/getPermissionList")
    public ModelAndView getPermissionList() {
        ModelAndView modelAndView = new ModelAndView("/system_permission/SystemPermission");
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/getPermissionListForPage")
    public ResponseBean getRoleListForPage() {
        List<SystemPermission> SystemPermission = systemPermissionService.getPermissionList();
        List<PermissionVo> roleVoList = new ArrayList<PermissionVo>();
        for (SystemPermission permission : SystemPermission){
            PermissionVo permissionVo = new PermissionVo();
            try {
                BeanUtils.copyProperties(permissionVo, permission);
            } catch (Exception e) {
                e.printStackTrace();
            }
            permissionVo.setAvailableVo(permission.getAvailable().equals("1")?"启用":"停用");
            roleVoList.add(permissionVo);
        }
        ResponseBean bean = new ResponseBean(0,"",roleVoList.size(),roleVoList);
        return bean;
    }

    @RequestMapping("/toPermissionAdd")
    public ModelAndView toPermissionAdd() {
        ModelAndView modelAndView = new ModelAndView("system_permission/SystemPermissionAdd");
        List<SystemPermission> list = systemPermissionService.getPermissionListByType();
//        Map<String,Object> map = new HashMap<String, Object>();
//        map.put("list",list);
        modelAndView.addObject("list",list);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/insertPermission")
    public Response insertPermission(SystemPermission vo){
        vo.setAvailable("1");
        if(null == vo.getParentid() || "".equals(vo.getParentid())){
            vo.setParentid(1L);
        }
        systemPermissionService.insertPermissionSelective(vo);
        return new Response("0","",vo);
    }

    @RequestMapping("/findPermissionById")
    public ModelAndView findPermissionById(String id){
        ModelAndView modelAndView = new ModelAndView("system_permission/SystemPermissionUpdate");
        List<SystemPermission> list = systemPermissionService.getPermissionListByType();
        SystemPermission permission = systemPermissionService.findPermissionById(id);
        PermissionVo permissionVo = new PermissionVo();
        try {
            BeanUtils.copyProperties(permissionVo, permission);
        } catch (Exception e) {
            e.printStackTrace();
        }
        permissionVo.setAvailableVo(permission.getAvailable().equals("1")?"启用":"停用");
        modelAndView.addObject("vo",permissionVo);
        modelAndView.addObject("list",list);
        return modelAndView;
    }

    @RequestMapping("/updatePermission")
    public void updatePermission(SystemPermission vo, HttpServletResponse response){
        systemPermissionService.updatePermission(vo);
        try {
            response.sendRedirect("/permission/getPermissionList");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ResponseBody
    @RequestMapping("deleteAllPermission")
    public Response deleteAllPermission(@RequestParam(value = "ids[]")String[] ids){
        systemPermissionService.deleteAllPermission(ids);
        for(String pid : ids){
            systemRolePermissionServiceImpl.deleteByPermissionId(pid);
        }
        return new Response("0","","");
    }


    @ResponseBody
    @RequestMapping("deletePermission")
    public Response deletePermission(String id){
        systemPermissionService.deletePermission(id);
        systemRolePermissionServiceImpl.deleteByPermissionId(id);
        return new Response("0","","");
    }
}
